package com.good.child.ui.adapter.ai;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.good.child.R;
import com.good.child.bean.ai.AIHelperHistoryBean;
import com.good.child.widgets.PrinterTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AiGenerateTextByImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<AIHelperHistoryBean> dataList;
    private onItemClick onItemClick;
    public static final int ITEM_TYPE_SEND = 1;
    public static final int ITEM_TYPE_RECEIVE = 2;

    public AiGenerateTextByImageAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<AIHelperHistoryBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_SEND) {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_generate_picture_by_image_send, parent, false);
            SendViewHolder myViewHolder = new SendViewHolder(inflater);
            return myViewHolder;
        } else {
            View inflater = LayoutInflater.from(context).inflate(R.layout.item_ai_message_receive, parent, false);
            ReceiveViewHolder myViewHolder = new ReceiveViewHolder(inflater);
            return myViewHolder;
        }
    }

    @Override
    public int getItemViewType(int position) {
        AIHelperHistoryBean bean = dataList.get(position);
        return bean.getModeType() == 2 ? ITEM_TYPE_RECEIVE : ITEM_TYPE_SEND;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }
        AIHelperHistoryBean bean = dataList.get(position);
        if (holder instanceof SendViewHolder) {
            SendViewHolder viewHolder = (SendViewHolder) holder;
            if (TextUtils.isEmpty(bean.getImageUrl())) {
                return;
            }
            Glide.with(context).load(bean.getImageUrl()).into(viewHolder.im);
            viewHolder.tv_content.setVisibility(TextUtils.isEmpty(bean.getContent()) ? View.GONE : View.VISIBLE);
            viewHolder.tv_content.setText(bean.getContent());
            viewHolder.im.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClick != null) {
                        onItemClick.onClickView(bean, viewHolder.im);
                    }
                }
            });
        } else {
            ReceiveViewHolder viewHolder = (ReceiveViewHolder) holder;
            if (bean.isNeedPrinter()) {
                viewHolder.tvContent.setPrintText(bean.getContent());
                viewHolder.tvContent.startPrint();
            } else {
                viewHolder.tvContent.stopPrint();
                viewHolder.tvContent.setText(bean.getContent());
            }
            viewHolder.clrootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClick != null) {
                        onItemClick.onClickView(bean, viewHolder.clrootView);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 1 : dataList.size();
    }

    class SendViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.im)
        ImageView im;
        @BindView(R.id.tv_content)
        TextView tv_content;

        public SendViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ReceiveViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_content)
        PrinterTextView tvContent;
        @BindView(R.id.tv_send)
        TextView tv_send;
        @BindView(R.id.clrootView)
        ConstraintLayout clrootView;

        public ReceiveViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onItemClick {
        void onClickView(AIHelperHistoryBean bean, View view);
    }
}
